/******************************************************************************/
/* Files to Include                                                           */
/******************************************************************************/

#if defined(__XC)
    #include <xc.h>        /* XC8 General Include File */
#elif defined(HI_TECH_C)
    #include <htc.h>       /* HiTech General Include File */
#elif defined(__18CXX)
    #include <p18cxxx.h>   /* C18 General Include File */
#endif

#if defined(__XC) || defined(HI_TECH_C)

//#include <stdint.h>        /* For uint8_t definition */
//#include <stdbool.h>       /* For true/false definition */

#endif

#include "stdtypedef.h"
#include "system.h"        /* System funct/params, like osc/peripheral config */
#include "app_init.h"          /* app_init funct/params, such as InitApp */
#include "app_ticks.h"
#include "app_io.h"
#include "periodic_task_exec.h"


/******************************************************************************/
/* app_init Global Variable Declaration                                           */
/******************************************************************************/
/*
T_UBYTE rub_Task_Tck_Exec_Flag = 0,
        rub_Task_5Tck_Exec_Flag = 0,
        rub_Task_10Tck_Exec_Flag = 0,
        rub_Task_20Tck_Exec_Flag = 0,
        rub_Task_200Tck_Exec_Flag = 0,
        rub_Task_1kTck_Exec_Flag = 0,
        rub_NewTickFlag = 0;
 */
union{
    struct{
        T_UBYTE     ALL;
    }by;
    struct{
        T_UBYTE     _1TCK   :1;
        T_UBYTE     _5TCK   :1;
        T_UBYTE     _10TCK  :1;
        T_UBYTE     _20TCK  :1;
        T_UBYTE     _200TCK :1;
        T_UBYTE     _1kTCK  :1;

        T_UBYTE     NEWTCK  :1;
    }bi;
}rs_TckFlags;
T_UWORD ruw_TickCounter = 0;
/* i.e. uint8_t <variable_name>; */

/******************************************************************************/
/* Main Program                                                               */
/******************************************************************************/

void main(void)
{
    /* Configure the oscillator for the device */
//    ConfigureOscillator();

    /* Initialize I/O and Peripherals for application */
    InitApp();

    /* TODO <INSERT app_init APPLICATION CODE HERE> */

    while(1)
    {
        CheckTicks();
        if(rs_TckFlags.bi._1TCK){
            rs_TckFlags.bi._1TCK = 0;
            periodic_task_tk();
        }
        if(rs_TckFlags.bi._5TCK){
            rs_TckFlags.bi._5TCK = 0;
            periodic_task_5tk();
        }
        if(rs_TckFlags.bi._10TCK){
            rs_TckFlags.bi._10TCK = 0;
            periodic_task_10tk();
        }
        if(rs_TckFlags.bi._20TCK){
            rs_TckFlags.bi._20TCK = 0;
            periodic_task_20tk();
        }
        if(rs_TckFlags.bi._200TCK){
            rs_TckFlags.bi._200TCK = 0;
            periodic_task_200tk();
        }
        if(rs_TckFlags.bi._1kTCK){
            rs_TckFlags.bi._1kTCK = 0;
            periodic_task_1ktk();
        }
    }

}